package com.api.shopapi.controller;


import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.*;
import com.api.shopapi.service.ElderlyDrugRecordService;
import com.api.shopapi.service.ElderlyService;
import com.api.shopapi.service.InstitutionStaffRelService;
import com.api.shopapi.service.StaffService;
import com.api.shopapi.util.Util;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.UUID;

/**
 * @author maochuan
 * 老人自带药品记录
 */
@RestController
@RequestMapping("ElderlyDrugRecord")
public class ElderlyDrugRecordController extends BaseController {

    @Autowired
    @Qualifier("ElderlyDrugRecordServiceImplOne")
    private ElderlyDrugRecordService elderlyDrugRecordService;

    @Autowired
    @Qualifier("StaffServiceImplOne")
    private StaffService staffService;

    @Autowired
    @Qualifier("ElderlyServiceImplOne")
    private ElderlyService elderlyService;


    @Autowired
    @Qualifier("InstitutionStaffRelServiceImplOne")
    private InstitutionStaffRelService institutionStaffRelService;


    @RequestMapping(value = "Add", method = RequestMethod.POST)
    public MyResult Insert(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            String elderlyCode = jsonObject.getString("elderlyCode");
            String remarks = jsonObject.getString("remarks");
            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/ElderlyDrugRecord/Add");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }
            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String institutionCode = institutionStaffRel.getInstitutionCode();


            String elderlyName = elderlyService.getByElderlyNameToElderlyCode(elderlyCode);
            ElderlyDrugRecord elderlyDrugRecord = new ElderlyDrugRecord();
            String elderlyDrugRecordCode = UUID.randomUUID().toString();
            elderlyDrugRecord.setElderlyDrugRecordCode(elderlyDrugRecordCode);
            elderlyDrugRecord.setElderlyCode(elderlyCode);
            elderlyDrugRecord.setElderlyName(elderlyName);
            elderlyDrugRecord.setRemarks(remarks);
            elderlyDrugRecord.setCreateTime(new Date());
            elderlyDrugRecord.setStaffCode(staffCode);
            elderlyDrugRecord.setInstitutionCode(institutionCode);

            elderlyDrugRecordService.insert(elderlyDrugRecord);
            Object data = new Object[]{};
            return success(data);
        } catch (Exception e) {
            insertError("/ElderlyDrugRecord/Add", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }


    @RequestMapping(value = "Page", method = RequestMethod.POST)
    public MyResult page(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            String elderlyName = jsonObject.getString("elderlyName");
            String startTime = jsonObject.getString("startTime");
            String endTime = jsonObject.getString("endTime");
            int pageIndex = jsonObject.getInteger("pageIndex");
            int pageSize = jsonObject.getInteger("pageSize");
            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/ElderlyDrugRecord/Page");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }
            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String institutionCode = institutionStaffRel.getInstitutionCode();

            endTime = Util.timeAddDay(endTime, 1);
            PageInfo<ElderlyDrugRecord> elderlyDrugRecordPageInfo = elderlyDrugRecordService.getPageList(pageIndex, pageSize, institutionCode, elderlyName, startTime, endTime);
            return success(elderlyDrugRecordPageInfo);
        } catch (Exception e) {
            insertError("/ElderlyDrugRecord/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }

}
